<?php

namespace app\common\model;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;

/**
 * 会员模型
 */
class Wechat
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名

    /**
     * (找人/找物/失物招领)支付准备
     * @param $data
     * @param $openid
     * @return array
     */
    public function WeixinPrePay($data, $openid) {
        $options = configNew();
        $app = new Application($options);
        $payment = $app->payment;
        $attributes = [
            'body' => $data['title'],
            'out_trade_no' => $data['order_sn'],
            'total_fee' => $data['money']*100,
            #'spbill_create_ip' => '***',
            'notify_url' => url('index/notify',[],true,true), // 回调地址
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];

        $order = new Order($attributes);
        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
            $json = $payment->configForJSSDKPayment($prepayId);
            return ['status' => 200, 'msg' => '', 'data' => $json];
        } else {
            return ['status' => 400, 'msg' => '调起支付失败，请稍后尝试'];
        }
    }
    /**
     * (vip)支付准备
     * @param $data
     * @param $openid
     * @return array
     */
    public function WeixinVipPay($data, $openid) {
        $options =configNew();
        $app = new Application($options);
        $payment = $app->payment;
        $attributes = [
            'body' => $data['title'],
            'out_trade_no' => $data['order_sn'],
            'total_fee' => $data['money']*100,
            #'spbill_create_ip' => '***',
            'notify_url' => url('index/vipnotify',[],true,true), // 回调地址
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];



        $order = new Order($attributes);
        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
            $json = $payment->configForJSSDKPayment($prepayId);
            return ['status' => 200, 'msg' => '', 'data' => $json];
        } else {
            return ['status' => 400, 'msg' =>'调起支付失败，请稍后尝试'];
        }
    }

    public function auctionplay($data, $openid){
        $options =configNew();
        $app = new Application($options);
        $payment = $app->payment;
        $attributes = [
            'body' => $data['title'],
            'out_trade_no' => $data['order_sn'],
            'total_fee' => $data['money']*100,
            #'spbill_create_ip' => '***',
            'notify_url' => url('index/bidnotify',[],true,true), // 回调地址
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];



        $order = new Order($attributes);
        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
            $json = $payment->configForJSSDKPayment($prepayId);
            return ['status' => 200, 'msg' => '', 'data' => $json];
        } else {
            return ['status' => 400, 'msg' =>'调起支付失败，请稍后尝试'];
        }
    }

    //消息模板推送
    public function template($templateId,$url,$data,$openid){
        $options =configNew();
        $app = new Application($options);
        $notice=$app->notice;
        $result=$notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($openid)->send();
        return $result;
    }
}
